/**
 * 
 */
package org.unisannio.picture.broadcast.remote;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;

/**
 * @author Fabio Melillo
 *
 */
public class ShowImage extends Activity {
	
	private static String TAG="ShowImage";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_image);
		// Get the image we want to work with from a URL
		String pic_path=getIntent().getStringExtra(MediaStore.EXTRA_OUTPUT);
		if (pic_path!=null){
    	Bitmap myBitmap = getBitmap(pic_path);

    	// find the width and height of the screen:
    	Display d = getWindowManager().getDefaultDisplay();
    	int x = d.getWidth();
    	int y = d.getHeight();

    	// get a reference to the ImageView component that will display the image:
    	ImageView imageView=new ImageView(this);

    	// scale it to fit the screen, x and y swapped because my image is wider than it is tall
    	Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);

    	// create a matrix object
    	Matrix matrix = new Matrix();
    	matrix.postRotate(90); // clockwise by 90 degrees

    	// create a new bitmap from the original using the matrix to transform the result
    	Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);

    	ImageView image=(ImageView) findViewById(R.id.imageView1);
    	image.setImageBitmap(rotatedBitmap);
		}
		else
			finish();
	}

	
	private Bitmap getBitmap(String path) {
		Log.d(TAG,path);
//		Uri uri = Uri.parse(path);
		InputStream in = null;
		try {
		    final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
		    in = new FileInputStream(new File(path));
//		    in = mContentResolver.openInputStream(uri);

		    // Decode image size
		    BitmapFactory.Options o = new BitmapFactory.Options();
		    o.inJustDecodeBounds = true;
		    BitmapFactory.decodeStream(in, null, o);
		    in.close();



		    int scale = 1;
		    while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > 
		          IMAGE_MAX_SIZE) {
		       scale++;
		    }
		    Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ",  orig-height: " + o.outHeight);

		    Bitmap b = null;
		    in = new FileInputStream(new File(path));
		    if (scale > 1) {
		        scale--;
		        // scale to max possible inSampleSize that still yields an image
		        // larger than target
		        o = new BitmapFactory.Options();
		        o.inSampleSize = scale;
		        b = BitmapFactory.decodeStream(in, null, o);

		        // resize to desired dimensions
		        int height = b.getHeight();
		        int width = b.getWidth();
		        Log.d(TAG, "1th scale operation dimenions - width: " + width + ",  height: " + height);

		        double y = Math.sqrt(IMAGE_MAX_SIZE
		                / (((double) width) / height));
		        double x = (y / height) * width;

		        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
		           (int) y, true);
		        b.recycle();
		        b = scaledBitmap;

		        System.gc();
		    } else {
		        b = BitmapFactory.decodeStream(in);
		    }
		    in.close();

		    Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " + 
		       b.getHeight());
		    return b;
		} catch (IOException e) {
		    Log.e(TAG, e.getMessage(),e);
		    return null;
		}
	}
}
